#include <chipKITEthernet.h>

/*
  This sketch is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This sketch is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this sketch; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
/************************************************************************/
/*  Revision History:							*/
/* 									*/
/* 8/08/2011 Digilent Inc. (KeithV)					*/
/* Updated for use with the chipKIT Max32 and chipKIT Network Shield	*/
/*									*/
/************************************************************************/

/*
  Web  Server
 
 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 
 created 18 Dec 2009
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe
 
 */

// Enter a MAC address and IP address for your controller below. 
// A zero MAC address means that the chipKIT MAC is to be used
byte mac[] = {  
  0x00, 0x04, 0xa3, 0x52, 0x90, 0x5b };

//       !!MODIFY THIS!!
// The IP address will be dependent on your local network:
byte ip[] = { 
  192,168,1,2 };

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
Server server(80);

void setup()
{
  // start the Ethernet connection and the server:
  //Ethernet.begin(mac, ip);
  Ethernet.begin();  // this will use DHCP
  server.begin();
  Serial.begin(9600);
  Serial.print("Server started");
}

void loop()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("");
          
          client.println("<html>");
          client.println("<head>"
                         "<title>Hemstyrning</title>"
                         "<meta name='viewport' content='width=device-width, initial-scale=1'>"
                         "<meta charset='utf-8' />"
                         "<link rel='stylesheet' href='http://192.168.1.71/jquery.mobile-1.0.min.css' />"
                         "<script type='text/javascript' src='http://192.168.1.71/jquery-1.6.4.min.js'></script>"
                         "<script type='text/javascript' src='http://192.168.1.71/jquery.mobile-1.0.min.js'>"
                         "</script>"
                         "</head>");
                         
          client.println("<body>");
          client.println("<!-- Start of first page -->"
                         "<div data-role='page' id='hem'>"
                           "<div data-role='header' data-position='inline'>"
                             "<h1>Hemstyrning 3000</h1>"
                             "<a href='index.html' data-icon='home' class='ui-btn-left'>Hem</a>"
                             "<span class='ui-title' />"
                           "</div><!-- /header -->"
                           "<div data-role='content'>"
                             "<h2>Sensorer</h2>"
                             "<p>Sensor 1 är: </p>"
                             "<p>Sensor 2 är: </p>"
                             "<p>Sensor 3 är: </p>"
                             "<p>Sensor 4 är: </p>"
                             "<div data-role='fieldcontain'>"
                               "<label for='slider'>Lampa 1</label>"
                               "<select name='slider' id='slider' data-role='slider'>"
                               "<option value='off'>Av</option><option value='on'>På</option>"
                               "</select>"
                             "</div>"
                             "<p><a href='#temp' data-transition='slide'>Temperaturer</a></p>"
                           "</div><!-- /content -->"
                         "</div><!-- /page -->");

          client.println("<!-- Start of second page -->"
                         "<div data-role='page' id='temp' data-theme='b'>"
                           "<div data-role='navbar'>"
                             "<ul>"
                               "<li><a href='#'>Ett</a></li>"
                               "<li><a href='#'>Två</a></li>"
                               "<li><a href='#'>Tre</a></li>"
                             "</ul>"
                           "</div><!-- /navbar -->"
                           "<div data-role='header'>"
                             "<h1>Hemstyrning - Temperatur</h1>"
                             "<span class='ui-title'/>"
                           "</div><!-- /header -->"
                           "<div data-role='content'>"
                             "<h2>Temperatur</h2>"
                             "<p>Kommer snart!</p>"
                             "<p><a href='#hem' data-transition='slide' data-direction='reverse'>Tillbaka</a></p>"
                           "</div><!-- /content -->"
                         "</div><!-- /page -->");
          
          client.println("<!-- Start of third page -->"
                         "<div data-role='page' id='test'>"
                           "<div data-role='header'>"
                             "<h1>TEST</h1>"
                             "<span class='ui-title'/>"
                           "</div><!-- /header -->"
                           "<div data-role='content'>"
                             "<h2>Test</h2>"
                             "<p>Test Test Test</p>"
                             "<p>Det här är en lång rad utan radbrytningar i HTML-koden. Allt det här står alltså på en rad. Nu är raden slut.</p>"
                             "<p>Här är en ny radmed radbrytning.</p>"
                             "<p><a href='#hem' data-transition='slide' data-direction='reverse'>Tillbaka</a></p>"
                           "</div><!-- /content -->"
                         "</div><!-- /page -->");
          client.println("</body>");
          client.println("</html>");
   
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}
